home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / template.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  2.4 KB  |  105 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  template.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_TEMPLATE_H
  16. #define LEDA_TEMPLATE_H
  17.  
  18.  
  19. // g++, bcc, and ztc allow to use template arguments as base classes,
  20. // cfront does not !
  21. // If template arguments cannot be used as base classes the old 
  22. // macro-based declare mechanism has to be used for data types 
  23. // with implementation parameters
  24.  
  25. #if defined(__GNUG__) || defined(__TURBOC__) || defined(__ZTC__) 
  26. #define __TEMPLATE_ARGS_AS_BASE__
  27. #else
  28. // cfront 
  29. #include <generic.h>
  30. #define declare3(_a,t1,t2,t3)    name2(_a,declare3)(t1,t2,t3)
  31. #endif
  32.  
  33.  
  34.  
  35. // g++ (version < 2.6), bcc, and ztc cannot handle template functions correctly
  36. // and we have to use the LEDA_TYPE_PARAMETER macro for class type arguments
  37. // to generate Copy, Clear, Access, ... functions.
  38.  
  39. #if (defined(__GNUG__) && __GNUC_MINOR__ < 6) || defined(__TURBOC__) || defined(__ZTC__) 
  40. #undef __TEMPLATE_FUNCTIONS__
  41.  
  42. #else
  43. #define __TEMPLATE_FUNCTIONS__
  44.  
  45. // function templates (default implementations) for Convert, Copy, Clear,
  46. // Access, Init, Int_Type, Type_Name, Print, Read, compare, ...
  47. // (to be used if there are no special versions defined in basic.h)
  48.  
  49.  
  50. template <class T>
  51. inline GenPtr Convert(const T& x) 
  52. { if (sizeof(T) == sizeof(GenPtr)) 
  53.      return *(GenPtr*)&x;
  54.   else 
  55.      return GenPtr(&x);
  56. }
  57.  
  58.  
  59. template <class T>
  60. inline GenPtr Copy(const T& x) 
  61. { if (sizeof(T) == sizeof(GenPtr)) 
  62.      return *(GenPtr*)&x;
  63.   else 
  64.      return new T(x);
  65.  }
  66.  
  67.  
  68. template <class T>
  69. inline void Clear(T& x) 
  70. { T* p = &x;
  71.   if (sizeof(T) > sizeof(GenPtr)) delete p; 
  72.  }
  73.  
  74.  
  75. template <class T>
  76. inline T& Access(T*, const GenPtr& p) 
  77. { if (sizeof(T) <= sizeof(GenPtr)) 
  78.      return *(T*)&p;
  79.   else 
  80.      return *(T*)p;
  81. }
  82.  
  83. template <class T> inline void  Init(T&) {}
  84.  
  85. template <class T> inline int   Int_Type(T*)  { return 0; }
  86.  
  87. template <class T> inline char* Type_Name(T*) { return "unknown"; }
  88.  
  89.  
  90. /*
  91. template <class T> inline void Print(const T&, ostream&) {}
  92. template <class T> inline void Read(T&, istream&) {}
  93.  
  94. template <class T>
  95. inline int compare(const T&, const T&)  
  96. { error_handler(1,string("compare for type `%s' undefined",Type_Name((T*)0)));  
  97.   return 0;
  98.  }
  99. */
  100.  
  101. #endif
  102.  
  103. #endif
  104.